package com.tanhua.mongodbManager.interceptor;


import com.tanhua.mongodbManager.domain.db.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 用户权限的拦截器
 */
@Component
public class UserLoginInterceptor implements HandlerInterceptor {

    /**
     * 前置增强
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断会话域中是否有user对象
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        //为空表示没有登录
        if (user == null) {
            //重定向到登录页面
            response.sendRedirect(request.getContextPath()+"/login.html");
            //不再继续
            return false;
        }
        //不为空就放行
        return true;
    }
}
